package com.sky.controller.users;

import com.sky.constant.JwtClaimsConstant;
import com.sky.context.BaseContext;
import com.sky.dto.*;
import com.sky.entity.People;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.PeopleService;
import com.sky.utils.JwtUtil;
import com.sky.vo.PeopleLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/people")
@Slf4j
@Api(tags = "用户相关接口")
public class PeopleController {
    @Autowired
    private JwtProperties jwtProperties;
    @Autowired
    private PeopleService peopleService;

    /**
     * 登录
     * @param peopleLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "用户登录")
    public Result<PeopleLoginVO> login(@RequestBody PeopleLoginDTO peopleLoginDTO){
        System.out.println("数据----->>"+peopleLoginDTO.getPhone());
        log.info("用户登录:{}",peopleLoginDTO);
        People people=peopleService.login(peopleLoginDTO);
        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, people.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);
            PeopleLoginVO peopleLoginVO = PeopleLoginVO.builder()
                .id(people.getId())
                .phone(people.getPhone())
                .token(token)
                .build();
        return Result.success(peopleLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation(value = "用户退出")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     * 用户注册
     * @param peopleDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "用户注册")
    public Result save(@RequestBody PeopleDTO peopleDTO){
        System.out.println("用户注册数据----->>"+peopleDTO);
        log.info("用户注册:{}",peopleDTO);
        Long count=peopleService.selectCount();
        int flag = peopleService.selectByPhone(peopleDTO.getPhone());
        if(flag==1){
            return Result.error("用户已存在");
        }
        else if(flag==0){
            peopleDTO.setId(count++);
            peopleService.save(peopleDTO);
        }
        return Result.success();
    }

    /**
     * 用户修改密码
     * @param updateUserPassword
     * @return
     */
    @PostMapping("/updatePassword")
    @ApiOperation(value = "用户修改密码")
    public Result updatePassword(@RequestBody UpdateUserPassword updateUserPassword){
        Boolean aBoolean = peopleService.updatePassword(updateUserPassword);
        if(aBoolean){
            return Result.success();
        } else {
            return Result.error("修改密码失败");
        }

    }

    /**
     * 用户修改信息
     * @param peopleDTO
     * @return
     */
    @PostMapping("/updateInfo")
    @ApiOperation(value = "用户修改信息")
    public Result update(@RequestBody PeopleDTO peopleDTO){
        log.info("用户修改信息:{}",peopleDTO);
        peopleService.update(peopleDTO);
        return Result.success();
    }

    /**
     * 根据Id查询用户信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID查询用户信息")
    public Result<People> selectById(@PathVariable Long id){
        System.out.println(BaseContext.getCurrentId());
        People people=peopleService.selectOne(id);
        return Result.success(people);
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询用户信息")
    public Result<PageResult> pageSelect(PeoplePageQueryDTO peoplePageQueryDTO){
        PageResult pageResult= peopleService.pageQuery(peoplePageQueryDTO);
        return Result.success(pageResult);
    }

    @DeleteMapping("/deleteUser/{id}")
    @ApiOperation(value = "删除膳食接口")
    public Result deleteUser(@PathVariable Long id){
        peopleService.deleteUser(id);
        return Result.success();
    }



}
